/*
 * @Author: Aaron Huang
 * @Date: 2022-10-19 13:06:23
 * @LastEditors: Aaron Huang
 * @LastEditTime: 2022-10-21 17:20:30
 * @Description: file content
 * @FilePath: \ias_ui\src\components\newTable\index.js
 * @Company: 沃尔玛
 */
import moment from 'moment'
export const initializeData = {
  data() {
    return {
      loading: false,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      searchData: {},
      total: 0,
      tableData: [],
      multipleSelection: []
    }
  },
  methods: {
    selectionChange(val) {
      this.multipleSelection = val
    }
  }
}

export const searchHeaderEvent = {
  methods: {
    handleSearch() {
      this.$refs.table && this.$refs.table.clearSelection()
      this.pageParams.pageNum = 1
      this.queryTableDataFun()
    },
    handleReset(v = {}) {
      this.searchData = v
      this.handleSearch()
    }
  }
}

export const initializeMethod = {
  methods: {
    selectionChange(val) {
      this.multipleSelection = val
    },
    handleQueryParams() {
      const data = JSON.parse(JSON.stringify({ ...this.searchData, ...this.pageParams }))
      return data
    },
    async queryTableDataFun() {
      try {
        this.loading = true
        const params = this.handleQueryParams()
        if (typeof params === 'string') return this.$message.error(params)
        const res = await this.queryTableDataApi(params)
        this.handleResult(res)
      } finally {
        this.loading = false
      }
    },
    handleResult({ code, result }) {
      if (code !== '0') return
      this.total = result.total || 0
      this.tableData = result.data || []
    },

    // 封api时候
    // export const purchaseReviewExport = (data, name) => {
    //   return exportFile(url, data, name)
    // }

    async handleExport() {
      try {
        this.loading = true
        const params = this.handleQueryParams()
        if (typeof params === 'string') return this.$message.error(params)
        const date = moment(new Date()).format('YYYY-MM-DD')
        await this.exportTableDataApi(params, `${this.exportFileName} ${date}`)
      } finally {
        this.loading = false
      }
    }
  },
  created() {
    this.queryTableDataFun()
  }
}

export const checkBoxFun = {
  methods: {
    // 校验批量数据
    checkQuantity(val, maxNum = 50) {
      const arr = Array.isArray(val) && val?.length ? val : false
      const multipleSelection = arr || this.multipleSelection
      if (multipleSelection.length === 0) {
        this.$message({
          type: 'error',
          message: `请选择需要操作的数据`
        })
      } else if (multipleSelection.length > maxNum) {
        this.$message({
          type: 'error',
          message: `批量任务数量不能超过${maxNum}条`
        })
        return false
      } else return multipleSelection
    },
    /**
     *判断批量操作是否超过数量
     *message, callBack, num = 50, dataList = false, key = 'id' dataList需要判断的数据不传或者false拿multipleSelection数组 isObj输出对象 KeyValuePair格式 [{key:原数据的key， valueKey得到的key}]
     */
    open(message, callBack, num = 50, dataList = false, key = 'id', isObj = false, KeyValuePair = []) {
      const arr = this.checkQuantity(dataList, num)
      if (!arr) return
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const params = arr.map((_) => {
            if (isObj) {
              const returnObj = {}
              KeyValuePair.forEach((item) => {
                returnObj[item.key] = _[item.valueKey]
              })
              return returnObj
            }
            return _[key]
          })
          await callBack(params)
        })
        .catch(() => {})
    }
  }
}
